package com.oceansoft.mobile.econsole.modules.member.service.impl;

import com.alibaba.fastjson.JSON;
import com.oceansoft.mobile.econsole.cache.redis.CacheBaseService;
import com.oceansoft.mobile.econsole.common.util.MD5Util;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.member.entity.Member;
import com.oceansoft.mobile.econsole.modules.member.persistence.IUserCacheMapper;
import com.oceansoft.mobile.econsole.modules.member.service.IUserCacheService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户缓存业务组件实现类
 *
 * @author andy
 * @time 15-1-21 下午4:13
 */
@Service
public class UserCacheServiceImpl extends CacheBaseService implements IUserCacheService {

    @Resource
    private IUserCacheMapper userCacheMapper;

    @Override
    public Result cacheUserToRedisByRange(String regStartTime, String regEndTime) {
        List<Member> members = userCacheMapper.findUserByRegTimeRange(regStartTime, regEndTime);
        int succNum = 0, total = members.size();
        try {
            for (Member member : members) {
                super.cache(MD5Util.getMD5String(member.getLoginId()), JSON.toJSONString(member));
                succNum++;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new Result(Result.SUCC, String.format("共缓存%1$d用户，成功%2$d,失败%3$d", total, succNum, total - succNum));
    }
}
